package cn.b.sky.shop.activiti.configuration;

import cn.b.sky.shop.activiti.listener.ProxyTaskListener;
import cn.b.sky.shop.activiti.listener.ProxyUserTaskBpmnParseHandler;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.impl.bpmn.parser.handler.UserTaskParseHandler;
import org.activiti.engine.impl.history.HistoryLevel;
import org.activiti.engine.parse.BpmnParseHandler;
import org.activiti.spring.SpringAsyncExecutor;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.restlet.ext.servlet.ServerServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Configuration
@AutoConfigureAfter(name={"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration", })
public class WorkflowConfiguration extends AbstractProcessEngineAutoConfiguration {

    @Autowired
    private List<BpmnParseHandler> customDefaultBpmnParseHandlers;

    @Bean
    @Override
    protected SpringProcessEngineConfiguration baseSpringProcessEngineConfiguration(DataSource dataSource,
                                                                                    PlatformTransactionManager platformTransactionManager, SpringAsyncExecutor springAsyncExecutor)
            throws IOException {
        SpringProcessEngineConfiguration springProcessEngineConfiguration =
                super.baseSpringProcessEngineConfiguration(dataSource, platformTransactionManager, springAsyncExecutor);

        List<BpmnParseHandler> customDefaultBpmnParseHandlers = new ArrayList<>();
        // 设置Event listener
        List<ActivitiEventListener>  eventListeners = new ArrayList<>();
        springProcessEngineConfiguration.setCustomDefaultBpmnParseHandlers(customDefaultBpmnParseHandlers);

        //springProcessEngineConfiguration.setEventListeners(eventListeners);
        // 禁用Activiti默认用户权限
        springProcessEngineConfiguration.setDbIdentityUsed(false);
        springProcessEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 不保留历史记录
        //springProcessEngineConfiguration.setDbHistoryUsed(true);
        springProcessEngineConfiguration.setHistoryLevel(HistoryLevel.NONE);
        springProcessEngineConfiguration.setActivityFontName("宋体");
        springProcessEngineConfiguration.setLabelFontName("宋体");
        return springProcessEngineConfiguration;
    }

    @Bean
    public BpmnParseHandler customDefaultBpmnParseHandler() {
        ProxyUserTaskBpmnParseHandler userTaskParseHandler = new ProxyUserTaskBpmnParseHandler();
        userTaskParseHandler.setTaskListenerId("humanTaskTaskListener");
        return userTaskParseHandler;
    }

    @Bean
    public ServletRegistrationBean explorerRestletServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
                new ServerServlet(), "/manage/activiti/*");
        Map<String, String> initParameters = Maps.newHashMap();
        initParameters.put("org.restlet.application", "cn.b.sky.shop.activiti.MyExplorerRestApplication");
        servletRegistrationBean.setInitParameters(initParameters);
        return servletRegistrationBean;
    }
}
